package com.quo.utils;

import com.order.hit.crossover.platform.common.bean.Notice;
import com.order.hit.crossover.platform.common.bean.order.FightOrderState;
import com.order.hit.crossover.platform.common.bean.order.OrderState;
import com.order.hit.crossover.platform.common.dao.FightOrderDao;
import com.order.hit.crossover.platform.common.dao.OrderDao;
import com.order.hit.crossover.platform.common.dao.QuotationDao;
import com.order.hit.crossover.platform.common.utils.PFResponse;
import com.order.hit.crossover.platform.common.utils.TimeChange;
import com.quo.mapper.QuotationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Lei
 * @version 0.1
 * @date 2020/8/19
 */
@Component
public class OverTimeJudge {

    @Autowired
    private QuotationMapper quotationMapper;


    /**
     * 判断报价单是否超时
     */
    public void judgeQuotationIfOverTime(){
        List<QuotationDao> list = this.quotationMapper.selectAll();

        for(QuotationDao quotationDao : list){
            String end_time = quotationDao.getEnd_time();
            String nowTime = new SimpleDateFormat("yyyy.MM.dd.HH").format(new Date());
            if (TimeChange.getDate(end_time).before(TimeChange.getDate(nowTime)) && quotationDao.getQuotedState().equalsIgnoreCase(OrderState.QUOTATION_DISPLAY.toString())){
                //表示为 订单超时
                quotationDao.setQuotedState(OrderState.QUOTATION_OVERTIME.toString());
                this.quotationMapper.update(quotationDao);

                //  生成通知信息
                String fail_info = "您的报价单(" + quotationDao.getGoodName() + ")已超时， 请前往报价单购买/出售查看。";;
                Notice fail_notice = new Notice(fail_info,0,0,quotationDao.getStoreId());
                new RestTemplate().exchange("http://notice:18083/Notice/add", HttpMethod.POST, new HttpEntity<>(fail_notice, new HttpHeaders()), PFResponse.class);

            }
        }
    }

}
